//
//  YNSApplePayVC.m
//  HNBaseProject
//
//  Created by TR on 2024/9/18.
//

#import "YNSApplePayVC.h"
#import "YRStoreManager.h"
@interface YNSApplePayVC ()<MxNavigationViewDelegate>

@property(nonatomic,strong)UIImageView*bgIamgeV;
@property(nonatomic,strong)MxPublicNav*nav;
@property(nonatomic,strong)UILabel*lab1;
@property(nonatomic,strong)UIImageView*imageV1;
@property(nonatomic,strong)UILabel*lab2;
@property(nonatomic,strong)UILabel*lab3;
@property(nonatomic,strong)UIImageView*imageV2;
@property(nonatomic,strong)UILabel*lab4;
@property(nonatomic,strong)UIImageView*imageV3;
@property(nonatomic,strong)UILabel*lab5;
@property(nonatomic,strong)UIImageView*imageV4;
@property(nonatomic,strong)UILabel*lab6;
@property(nonatomic,strong)UIControl*weekCtl;
@property(nonatomic,strong)UILabel*weekTLab;
@property(nonatomic,strong)UILabel*weekMLab;
@property(nonatomic,strong)UIImageView*weekImageV;
@property(nonatomic,strong)UIControl*yearCtl;
@property(nonatomic,strong)UILabel*yearTLab;
@property(nonatomic,strong)UILabel*yearMLab;
@property(nonatomic,strong)UIImageView*yearImageV;
@property(nonatomic,strong)UIButton*payBtn;
@property(nonatomic,assign) NSInteger type;
@end

@implementation YNSApplePayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor blackColor];
    self.type = 1;
    [self setUI];
}

-(void)setUI{
    
    self.bgIamgeV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    self.bgIamgeV.image = [UIImage imageNamed:@"pay_bg"];
    self.bgIamgeV.contentMode = UIViewContentModeScaleAspectFit|UIViewContentModeTop;
    [self.view addSubview:self.bgIamgeV];
    
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"Purse";
    self.nav.titleLab.textColor = [UIColor whiteColor];
    self.nav.delegate = self;
    self.nav.line.hidden = YES;
    [self.nav.backButton setImage:[UIImage imageNamed:@"back_w"] forState:UIControlStateNormal];
    self.nav.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.nav];
    
    self.lab1 = [[UILabel alloc]init];
    self.lab1.text = @"Get Full Access";
    self.lab1.textColor = [UIColor whiteColor];
    self.lab1.font = [UIFont systemFontOfSize:31 weight:UIFontWeightBold];
    self.lab1.numberOfLines = 0;
    [self.view addSubview:self.lab1];
    [self.lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kNavBarHeight+200);
        make.left.offset(15.5);
    }];
    
    self.imageV1 = [[UIImageView alloc]init];
    self.imageV1.image = [UIImage imageNamed:@"pay_g"];
    [self.view addSubview:self.imageV1];
    [self.imageV1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab1.mas_bottom).offset(27);
        make.left.offset(15.5);
        make.width.offset(17);
        make.height.offset(11);
    }];
    
    self.lab2 = [[UILabel alloc]init];
    self.lab2.text = @"1000 weekly dimonds";
    self.lab2.textColor = [UIColor whiteColor];
    self.lab2.font = [UIFont systemFontOfSize:18];
    self.lab2.numberOfLines = 0;
    [self.view addSubview:self.lab2];
    [self.lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab1.mas_bottom).offset(25);
        make.left.equalTo(self.imageV1.mas_right).offset(2);
    }];
    
    self.lab3 = [[UILabel alloc]init];
    self.lab3.text = @"(1 dimond per image & video frame)";
    self.lab3.textColor = RGBA_COLOR(255, 255, 255, 0.5);
    self.lab3.font = [UIFont systemFontOfSize:12];
    self.lab3.numberOfLines = 0;
    [self.view addSubview:self.lab3];
    [self.lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab2.mas_bottom).offset(1);
        make.left.equalTo(self.imageV1.mas_right).offset(2);
    }];
    
    self.imageV2 = [[UIImageView alloc]init];
    self.imageV2.image = [UIImage imageNamed:@"pay_g"];
    [self.view addSubview:self.imageV2];
    [self.imageV2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab3.mas_bottom).offset(17);
        make.left.offset(15.5);
        make.width.offset(17);
        make.height.offset(11);
    }];
    
    self.lab4 = [[UILabel alloc]init];
    self.lab4.text = @"AI hugging & latest trends";
    self.lab4.textColor = [UIColor whiteColor];
    self.lab4.font = [UIFont systemFontOfSize:18];
    self.lab4.numberOfLines = 0;
    [self.view addSubview:self.lab4];
    [self.lab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab3.mas_bottom).offset(15);
        make.left.equalTo(self.imageV2.mas_right).offset(2);
    }];
    
    self.imageV3 = [[UIImageView alloc]init];
    self.imageV3.image = [UIImage imageNamed:@"pay_g"];
    [self.view addSubview:self.imageV3];
    [self.imageV3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab4.mas_bottom).offset(17);
        make.left.offset(15.5);
        make.width.offset(17);
        make.height.offset(11);
    }];
    
    self.lab5 = [[UILabel alloc]init];
    self.lab5.text = @"Faster video processing";
    self.lab5.textColor = [UIColor whiteColor];
    self.lab5.font = [UIFont systemFontOfSize:18];
    self.lab5.numberOfLines = 0;
    [self.view addSubview:self.lab5];
    [self.lab5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab4.mas_bottom).offset(15);
        make.left.equalTo(self.imageV3.mas_right).offset(2);
    }];
    
    self.imageV4 = [[UIImageView alloc]init];
    self.imageV4.image = [UIImage imageNamed:@"pay_g"];
    [self.view addSubview:self.imageV4];
    [self.imageV4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab5.mas_bottom).offset(17);
        make.left.offset(15.5);
        make.width.offset(17);
        make.height.offset(11);
    }];
    
    self.lab6 = [[UILabel alloc]init];
    self.lab6.text = @"Exclusive effects & styles";
    self.lab6.textColor = [UIColor whiteColor];
    self.lab6.font = [UIFont systemFontOfSize:18];
    self.lab6.numberOfLines = 0;
    [self.view addSubview:self.lab6];
    [self.lab6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab5.mas_bottom).offset(15);
        make.left.equalTo(self.imageV4.mas_right).offset(2);
    }];
    
    self.weekCtl = [[UIControl alloc]init];
    self.weekCtl.layer.masksToBounds = YES;
    self.weekCtl.layer.cornerRadius = 10;
    self.weekCtl.layer.borderWidth = 2;
    self.weekCtl.layer.borderColor = [UIColor colorWithHexString:@"#1C1C1E"].CGColor;
    self.weekCtl.tag = 0;
    [self.weekCtl addTarget:self action:@selector(choseClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.weekCtl];
    [self.weekCtl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lab6.mas_bottom).offset(42);
        make.left.offset(15.5);
        make.right.offset(-15.5);
        make.height.offset(65);
    }];
    
    self.weekImageV = [[UIImageView alloc]init];
    self.weekImageV.image = [UIImage imageNamed:@"pay_chose_u"];
    [self.weekCtl addSubview:self.weekImageV];
    [self.weekImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.weekCtl.mas_centerY).offset(0);
        make.left.equalTo(self.weekCtl.mas_left).offset(18);
        make.width.offset(28);
        make.height.offset(28);
    }];
    
    self.weekTLab = [[UILabel alloc]init];
    self.weekTLab.text = @"Weekly";
    self.weekTLab.textColor = [UIColor colorWithHexString:@"#ABABAC"];
    self.weekTLab.font = [UIFont systemFontOfSize:13];
    [self.weekCtl addSubview:self.weekTLab];
    [self.weekTLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.weekImageV.mas_top).offset(0);
        make.left.equalTo(self.weekImageV.mas_right).offset(5);
    }];
    
    self.weekMLab = [[UILabel alloc]init];
    self.weekMLab.text = @"$6.99/week";
    self.weekMLab.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    self.weekMLab.font = [UIFont systemFontOfSize:17 weight:UIFontWeightBold];
    [self.weekCtl addSubview:self.weekMLab];
    [self.weekMLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.weekTLab.mas_bottom).offset(0);
        make.left.equalTo(self.weekImageV.mas_right).offset(5);
    }];
    
    self.yearCtl = [[UIControl alloc]init];
    self.yearCtl.layer.masksToBounds = YES;
    self.yearCtl.layer.cornerRadius = 10;
    self.yearCtl.layer.borderWidth = 2;
    self.yearCtl.tag = 1;
    [self.yearCtl addTarget:self action:@selector(choseClick:) forControlEvents:UIControlEventTouchUpInside];
    self.yearCtl.layer.borderColor = [UIColor colorWithHexString:@"#00B2F3"].CGColor;
    [self.view addSubview:self.yearCtl];
    [self.yearCtl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.weekCtl.mas_bottom).offset(15);
        make.left.offset(15.5);
        make.right.offset(-15.5);
        make.height.offset(65);
    }];
    
    self.yearImageV = [[UIImageView alloc]init];
    self.yearImageV.image = [UIImage imageNamed:@"pay_chose_c"];
    [self.yearCtl addSubview:self.yearImageV];
    [self.yearImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.yearCtl.mas_centerY).offset(0);
        make.left.equalTo(self.yearCtl.mas_left).offset(18);
        make.width.offset(28);
        make.height.offset(28);
    }];
    
    self.yearTLab = [[UILabel alloc]init];
    self.yearTLab.text = @"Yearly";
    self.yearTLab.textColor = [UIColor colorWithHexString:@"#ABABAC"];
    self.yearTLab.font = [UIFont systemFontOfSize:13];
    [self.yearCtl addSubview:self.yearTLab];
    [self.yearTLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.yearImageV.mas_top).offset(0);
        make.left.equalTo(self.yearImageV.mas_right).offset(5);
    }];
    
    self.yearMLab = [[UILabel alloc]init];
    self.yearMLab.text = @"$0.75/week";
    self.yearMLab.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    self.yearMLab.font = [UIFont systemFontOfSize:17 weight:UIFontWeightBold];
    [self.yearCtl addSubview:self.yearMLab];
    [self.yearMLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.yearTLab.mas_bottom).offset(0);
        make.left.equalTo(self.yearImageV.mas_right).offset(5);
    }];
    
    UIView *whiteBg = [[UIView alloc] init];
    [self.yearCtl addSubview:whiteBg];
    whiteBg.backgroundColor = [UIColor whiteColor];
    whiteBg.layer.cornerRadius = 5;
    whiteBg.layer.masksToBounds = YES;
    [whiteBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-7.5);
        make.width.mas_equalTo(kRealValue(75));
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(self.yearCtl);
    }];
    
    UILabel *topLab = [[UILabel alloc] init];
    topLab.backgroundColor = [UIColor colorWithHexString:@"#00B2F3"];
    [whiteBg addSubview:topLab];
    topLab.text = @"SPECIAL OFFER";
    topLab.textColor = [UIColor whiteColor];
    topLab.font = kFontBold(8);
    topLab.textAlignment = NSTextAlignmentCenter;
    [topLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.mas_equalTo(17);
    }];
    
    UILabel *bottomLab = [[UILabel alloc] init];
    [whiteBg addSubview:bottomLab];
    bottomLab.text = @"%90 off!!";
    bottomLab.textColor = [UIColor colorWithHexString:@"#000000"];
    bottomLab.font = kFontBold(13);
    bottomLab.textAlignment = NSTextAlignmentCenter;
    [bottomLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(17);
    }];
    
    self.payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.payBtn setTitle:@"Continue" forState:UIControlStateNormal];
    [self.payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.payBtn.backgroundColor = [UIColor colorWithHexString:@"#00B0F0"];
    self.payBtn.layer.masksToBounds = YES;
    self.payBtn.layer.cornerRadius = 10;
    [self.payBtn addTarget:self action:@selector(playClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.payBtn];
    [self.payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.yearCtl.mas_bottom).offset(15);
        make.left.offset(15.5);
        make.right.offset(-15.5);
        make.height.offset(55);
    }];
}

#pragma mark ---MxNavigationViewDelegate-----
- (void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

//支付
-(void)playClick{
    
    NSString *diamond_set_id = @"";
    if (self.type == 0) {
        diamond_set_id = @"aivideo_recharge_07";
    } else {
        diamond_set_id = @"aivideo_recharge_08";
    }
    MJWeakSelf
    [[UIApplication sharedApplication].keyWindow makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/diamond.diamond/diamondOrderSn" params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        if([dict[@"code"] intValue]==1){
            NSString *order_sn = dict[@"data"][@"order_sn"];//订单号
            
            [[YRStoreManager shareManager] startPurchaseWithID:diamond_set_id orderId:order_sn isRechange:NO completeHandle:^(IAPPurchType type, NSString * _Nonnull message) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication].keyWindow hideToastActivity];
                    if (type == IAPPurchSuccess) {
                        [weakSelf.view xjmakeToast:NSLocalizedString(@"购买成功", nil)];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"buysuccess" object:nil];
                    } else {
                        if (type == IAPPurchCancel) {
                            [weakSelf.view xjmakeToast:message];
                        } else {
                            [weakSelf.view xjmakeToast:message];
                        }
                    }
                });
            }];
        } else {
            [[UIApplication sharedApplication].keyWindow hideToastActivity];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
        
}
//选择
-(void)choseClick:(UIControl*)ctrol {
    if (ctrol.tag == 0) {
        self.type = 0;
        //选择了周
        self.weekCtl.layer.borderColor = [UIColor colorWithHexString:@"#00B2F3"].CGColor;
        self.weekImageV.image = [UIImage imageNamed:@"pay_chose_c"];
        self.yearCtl.layer.borderColor = [UIColor colorWithHexString:@"#1C1C1E"].CGColor;
        self.yearImageV.image = [UIImage imageNamed:@"pay_chose_u"];
    } else {
        self.type = 1;
        self.yearCtl.layer.borderColor = [UIColor colorWithHexString:@"#00B2F3"].CGColor;
        self.yearImageV.image = [UIImage imageNamed:@"pay_chose_c"];
        self.weekCtl.layer.borderColor = [UIColor colorWithHexString:@"#1C1C1E"].CGColor;
        self.weekImageV.image = [UIImage imageNamed:@"pay_chose_u"];
    }
}
@end
